
import * as Router from 'koa-router'
import * as companyService from '../services/company.service'
import { badParams } from '../stats'
import Joi = require('joi')
const router = new Router({
  prefix: '/api/company'
})

// 首页：热门公司
router.post('/home/hotCompany', async ctx => {
  const result = await companyService.list()
  result.sort(function (a, b) {
    return Number(b.like) - Number(a.like)
  })
  ctx.body = {
    stat: 'OK',
    result
  }
})

// 获取公司列表：通过token查
router.post('/admin/companylist', async ctx => {
  const token = ctx.cookies.get('token')
  const rows = await companyService.companylist(token)
  ctx.body = {
    stat: 'OK',
    rows
  }
})

// 从职位详细页跳转到公司详情页：根据公司id查询
router.post('/detail', async ctx => {
  const token = ctx.cookies.get('token')
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value } = schema.validate(ctx.request.body)
  if (value.id === 'token') {
    const result = await companyService.findOneCompany(token)
    ctx.body = {
      stat: 'OK',
      result
    }
  } else {
    const result = await companyService.findOne(value.id)
    ctx.body = {
      stat: 'OK',
      result
    }
  }
})

// 查找某个公司下的所有职位
router.post('/detail/allrecruit', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value } = schema.validate(ctx.request.body)
  const list = await companyService.listRecruit(value.id)
  ctx.body = {
    stat: 'OK',
    list
  }
})

// 管理端
// 添加公司：公司创始人id，是否禁用由超级管理员来处理
router.post('/admin/add', async ctx => {
  const token = ctx.cookies.get('token')
  const schema = Joi.object({
    companyShortName: Joi.string().required(),
    companyFullName: Joi.string(),
    isDisabled: Joi.number(),
    companySize: Joi.number().required(),
    industryField: Joi.string().required(),
    time: Joi.string(),
    updateAt: Joi.string(),
    logo: Joi.string().required(),
    financeStage: Joi.number().required(),
    describe: Joi.string().required()
  })

  const { value, error } = schema.validate(ctx.request.body)
  value.time = new Date().toLocaleDateString()
  value.isDisabled = 2
  value.updateAt = value.time
  if (error) throw badParams(error.message)
  const result = await companyService.add(value, token)
  ctx.body = {
    stat: 'OK',
    _id: result
  }
})

// 编辑公司
router.post('/admin/update', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required(),
    companySize: Joi.number(),
    companyFullName: Joi.string(),
    industryField: Joi.string(),
    logo: Joi.string(),
    financeStage: Joi.number(),
    describe: Joi.string()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  value.updateAt = new Date().toLocaleDateString()
  await companyService.update(value.id, value)
  ctx.body = {
    stat: 'OK'
  }
})

// 删除公司
router.post('/admin/remove', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  await companyService.remove(value.id)
  ctx.body = {
    stat: 'OK'
  }
})

router.post('/status', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  await companyService.status(value.id)
  ctx.body = {
    stat: 'OK'
  }
})

export default router
